import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class InsertSort {

    public static void main(String[] args) {
        List<User> list = new ArrayList<User>();
        User u1 = new User(1, 15);
        User u2 = new User(2, 10);
        User u3 = new User(3, 18);
        User u4 = new User(4, 8);
        User u5 = new User(5, 11);
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        InsertSort.insertSort(list);
    }

    private static void insertSort(List<User> list) {
        for (int i = 1; i < list.size() ; i++) {

            //第二个对象
            User userI = list.get(i);
            int age= userI.getAge();
            int id= userI.getId();

            //第一个对象
            int j = i-1;
            User userJ = list.get(j);
            //比较对象
            while(j>=0&&userJ.getAge()>age){
                 userJ = list.get(j);
                 // 前后交换
                // 交换
                list.set(j+1,userJ);
                //减减后前后在交换  也就是插在左边还是右边
                j--;
            }
            User temp = new User(id, age);
            list.set(j+1,temp);

        }
        System.out.println(JSONObject.toJSONString(list));
    }
}
